#pragma once
#ifndef __FLOGGER_H__
#define __FLOGGER_H__

#include <stdio.h>
#include <string>
#include "sync/cslock.h"

#ifdef USE_LOGGER_LOG4CPP
#include "log4cpp/Category.hh"

#define	FLOG_Uninitialize() log4cpp::Category::shutdown()
#else
#define	FLOG_Uninitialize()
#endif

#define LOG_SUM 100

/**
 */
class filelog
{
public:
	filelog(void);
	virtual	~filelog(void);

public:
  typedef 
  enum _logstyle
  {
    with_fixedsize = 0, /*!< Default style */
    with_hour = 1, /*!< Write log file name with hour format */ 
  } logstyle;

public:
  /** \brief Open log with specified name and log style
   *
   * max_file_num  --- maximum number of log files
   * append 是否是追加日志而非新建
   */
	bool open( const char* name,logstyle style=with_fixedsize,bool append=false);
	bool close();
  void enable( bool enable );
  #ifdef WIN32
    bool write_log( int evt, const char *fileName, const char *funcName, int lineNum, const char* str, ... );
  #else
    bool write_log( int evt, const char* str, ... );
  #endif
	bool write_log_console( int evt, const char* str, ... );

public:
  void Flag(unsigned long flag);
  void Path(const char* path, unsigned int max_file_num = 0);
  #ifdef WIN32
  void Path(const wchar_t* path, unsigned int max_file_num = 0);
  #endif
  void MaxSize(unsigned long size);
  void Split(const char spliter);
  void SetFileMaxCount(unsigned short usFileMaxCount);
  void SetEnableRealTimeOutput(bool enabled);

private:
  /** \brief Debug
   *
   */
  void debug_output( const char* str );

  /** \brief Write log data to file
   *
   */
  bool write_file_with_fixedsize();

  /** \brief Write log data with current hour
   *
   */
  void write_file_with_hour( const std::string& str );

  /** \brief Get Current file by hour 
   *
   */
  void current_file_byhour();

private:
	int m_nOpenFailedCount;
	bool m_bEnable;
	unsigned long m_nfsize;
	unsigned long m_nmaxfsize;
	const unsigned long m_nmaxBuffsize;
#ifdef WIN32
	std::wstring m_path;
#else
	std::string m_path;
#endif
	std::string m_name;
	int  m_nMaxFileNum; // maximum number of log files
//	void loadcfg();
	unsigned long m_nflag;
	//enum {max_file_size = 16*1024*1024 };
	int m_nbuf;	
	std::string m_buf[2];
	CMutexLock m_bufLock;
	CMutexLock m_fileLock;
	FILE *m_fstream;
  bool use_debug_file; /*!< Check the const file  (C:\\sunlogin.debug) is exists */
  char spliter_;
  logstyle style_;
  std::string current_fname_;
  unsigned short m_fileMaxCount; 

#ifdef WIN32
  bool m_enable_rt_output;
  timeval m_last_find_logwindow_time;
  HWND m_rt_logview_hwnd;
#endif
};

class flogger 
  : public filelog
{
public:
  static flogger &GetInitance();

public:
	flogger();

public:
	void loadcfg(const char* path = 0);
	void loadcfg(const char* lpstrPath, unsigned long nFlag, unsigned long nMaxSize, unsigned short usFileMaxCount = 0);

private:
	static flogger s_logger;
};

#endif // __FLOGGER_H__

